package com.example.mybiadushoujiweishi.activity.acivitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;


import com.example.mybiadushoujiweishi.R;
import com.example.mybiadushoujiweishi.activity.acivitys.adapter.RecyclerviewAdapter;
import com.example.mybiadushoujiweishi.activity.acivitys.base.BaseActivity;
import com.example.mybiadushoujiweishi.activity.acivitys.entity.BBX;
import com.example.mybiadushoujiweishi.activity.acivitys.listener.OnRecyclerItemClickListener;
import com.example.mybiadushoujiweishi.activity.acivitys.utils.DividerGridItemDecoration;
import com.example.mybiadushoujiweishi.activity.acivitys.utils.MyApplication;
import com.example.mybiadushoujiweishi.activity.acivitys.utils.MyItemTouchCallback;
import com.example.mybiadushoujiweishi.activity.db.BBXDao;
import com.example.mybiadushoujiweishi.activity.db.DaoSession;


import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

import butterknife.BindView;

public class BBXActivity extends BaseActivity implements MyItemTouchCallback.OnDragListener{
    static DaoSession daoSession;
    List<BBX> bbxList;
    RecyclerviewAdapter rvdapter;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    private ItemTouchHelper itemTouchHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.activity_bbx);
        /*MyApplication myApplication=new MyApplication();
        myApplication.initDatatoTable();*/
        data();
        //布局管理器
       // LinearLayoutManager line = new LinearLayoutManager(this);//线性布局管理器
       GridLayoutManager line = new GridLayoutManager(this,4);
        recyclerview.setLayoutManager(line);
        recyclerview.addItemDecoration(new DividerGridItemDecoration(this));
        //传输局
        rvdapter = new RecyclerviewAdapter(bbxList);
       itemTouchHelper=new ItemTouchHelper(new MyItemTouchCallback(rvdapter).setOnDragListener(this));
      recyclerview.addOnItemTouchListener(new OnRecyclerItemClickListener(recyclerview){

          @Override
          public void onLongClick(RecyclerView.ViewHolder vh) {
              super.onLongClick(vh);
              if (vh.getLayoutPosition()!=bbxList.size()-1) {
                  itemTouchHelper.startDrag(vh);
               //   VibratorUtil.Vibrate(BBXActivity.this, 70);
                 // itemTouchHelper.startDrag(vh); //震动70ms
              }
          }

          @Override
          public void onItemClick(RecyclerView.ViewHolder vh) {
              super.onItemClick(vh);
             BBX item = bbxList.get(vh.getLayoutPosition());
          //    Toast.makeText(BBXActivity.this,item.getId()+" "+item.getName(),Toast.LENGTH_SHORT).show();
             switch (item.getName()){
                 case "家人守护":
                     startActivity(new Intent(BBXActivity.this,FamilyguardActivity.class));
                     break;

                 case "回首页":
                     startActivity(new Intent(BBXActivity.this,MainActivity.class));
                     break;
             }

          }
      });
        itemTouchHelper.attachToRecyclerView(recyclerview);

        System.out.println(bbxList.size() + "oncreate=======bbxlist");
        //绑定到适配器
        recyclerview.setAdapter(rvdapter);


    }

    @Override
    public int getContentView() {
        return R.layout.activity_bbx;
    }

    @Override
    public void initData() {

    }


    public void data() {

        BBXDao bbxDao = MyApplication.getDaoSession().getBBXDao();
        QueryBuilder<BBX> qb = bbxDao.queryBuilder();
        bbxList = qb.list();

        System.out.println(bbxList.size() + "}}}}}}}}}}");

    }


    @Override
    public void onFinishDrag() {

    }
}
